import vue from 'vue'
import shareComponent from './share.vue'
// 返回一个 扩展实例构造器

const ShareConstructor = vue.extend(shareComponent)
var shareDom = null

export const showShare = (type) => {
  var loadingElem = document.querySelector('#zh-share')
  if (loadingElem === null) {
    shareDom = new ShareConstructor({
      el: document.createElement('div'),
      data() {
        return {
          showWrap: false, // 是否显示组件
          showContent: false // 作用:在隐藏组件之前,显示隐藏动画
        }
      }
    })

    document.body.appendChild(shareDom.$el)

    if (type === 'open') {
      shareDom.showWrap = true
    }
  }

  if (type === 'close') {
    shareDom.showWrap = false
    document.body.removeChild(shareDom.$el)
  }
}

// 注册为全局组件的函数
export const registryShare = () => {
  vue.prototype.$share = showShare
  vue.prototype.$share['open'] = () => {
    return showShare('open')
  }
  vue.prototype.$share['close'] = () => {
    return showShare('close')
  }
}

